<?php

  class saparcelplus {
    var $code, $title, $description, $enabled;

// class constructor
    function saparcelplus() {
    /**************************************************************************
    *                             Constructor                                 *
    **************************************************************************/
      $this->code = 'saparcelplus';
      $this->title = MODULE_SHIPPING_SAPARCELPLUS_TEXT_TITLE;
      $this->description = MODULE_SHIPPING_SAPARCELPLUS_TEXT_DESCRIPTION;
      $this->sort_order = MODULE_SHIPPING_SAPARCELPLUS_SORT_ORDER;
      $this->icon = '';
      $this->tax_class = MODULE_SHIPPING_SAPARCELPLUS_TAX_CLASS;
      $this->enabled = ((MODULE_SHIPPING_SAPARCELPLUS_STATUS == 'True') ? true : false);

    }

// class methods
    function quote($method = '') {

      global $order, $shipping_weight;
      $error = false;

      $dest_country = $order->delivery['country']['iso_code_2'];
      if ($dest_country != 'ZA') {
          return false;   // 'ParcelPlus is only available for destinations in South Africa.';
      }

      if ($shipping_weight > MODULE_SHIPPING_SAPARCELPLUS_MAX_WEIGHT) { 
        $this->quotes = array('id' => $this->code,
                              'module' => MODULE_SHIPPING_SAPARCELPLUS_TEXT_TITLE,
                              'methods' => array(array('title' => $this->quotes['error'])));
        $this->quotes['error'] = $shipping_weight . ' ' . 
                                 MODULE_SHIPPING_SAPARCELPLUS_TEXT_UNITS . ' ' . 
                                 MODULE_SHIPPING_SAPARCELPLUS_EXCEEDED_WEIGHT . ' ' . 
                                 MODULE_SHIPPING_SAPARCELPLUS_MAX_WEIGHT . ' ' . 
                                 MODULE_SHIPPING_SAPARCELPLUS_TEXT_UNITS;
        return $this->quotes;
      }

      if ($shipping_weight <= MODULE_SHIPPING_SAPARCELPLUS_ECONOPARCEL_MAX_WEIGHT) {
         // do Econoplus calculations because the parcel is small
         $method = 'ECONOPARCEL';
      } else {
         $method = 'PARCELPLUS';
      }
      $shipping_cost = 0;
      $shipping_method = '';
      $order_total = $order->info['total'];

      if (constant('MODULE_SHIPPING_SAPARCELPLUS_'.$method.'_INSURANCE_RATE') > 0) {
         if ($order_total > MODULE_SHIPPING_SAPARCELPLUS_INSURANCE_MAXIMUM) {
            $order_total = MODULE_SHIPPING_SAPARCELPLUS_INSURANCE_MAXIMUM;
            $shipping_method .= 'Only the first ' . MODULE_SHIPPING_SAPARCELPLUS_INSURANCE_MAXIMUM . ' of the parcel value will be insured.';
         }
         $shipping_cost =  $order_total
                           * constant('MODULE_SHIPPING_SAPARCELPLUS_'.$method.'_INSURANCE_RATE')
                           / 100; // percent
         if ($shipping_cost < MODULE_SHIPPING_SAPARCELPLUS_INSURANCE_MINIMUM) $shipping_cost = MODULE_SHIPPING_SAPARCELPLUS_INSURANCE_MINIMUM;
      }
      $shipping_cost += constant('MODULE_SHIPPING_SAPARCELPLUS_'.$method.'_BASE_RATE');
      $shipping_cost += constant('MODULE_SHIPPING_SAPARCELPLUS_'.$method.'_UNIT_RATE')
                        * ceil(($shipping_weight - MODULE_SHIPPING_SAPARCELPLUS_ECONOPARCEL_MAX_WEIGHT) / 1000 );
      $shipping_cost += MODULE_SHIPPING_SAPARCELPLUS_HANDLING_FEE;

      $shipping_method .= constant('MODULE_SHIPPING_SAPARCELPLUS_TEXT_'.$method.'_WAY') . ' ' . 
                          $shipping_weight . ' ' . 
                          MODULE_SHIPPING_SAPARCELPLUS_TEXT_UNITS . ' - ' .
                          MODULE_SHIPPING_SAPARCELPLUS_TEXT_DELIVERYTIME;
                                   
      $this->quotes = array(
        'id' => $this->code,
        'module' => MODULE_SHIPPING_SAPARCELPLUS_TEXT_TITLE,
        'methods' => array(
          array(
             # 'id' => $this->code,
            'id' => $method,
            'title' => $shipping_method,
            'cost' => $shipping_cost)));

      if ($this->tax_class > 0) {
        $this->quotes['tax'] = tep_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
      }

      if (tep_not_null($this->icon)) $this->quotes['icon'] = tep_image($this->icon, $this->title);
      if ($error == true) $this->quotes['error'] = MODULE_SHIPPING_SAPARCELPLUS_INVALID_ZONE;

      return $this->quotes;
    }

    function check() {
    /**************************************************************************
    *                                 Check                                   *
    **************************************************************************/
      if (!isset($this->_check)) {
        $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_SAPARCELPLUS_STATUS'");
        $this->_check = tep_db_num_rows($check_query);
      }
      return $this->_check;
    }

    function install() {
    /**************************************************************************
    *                               Install                                   *
    * Purpose: Creates the correct keys in the configuration table for 
    *          automatic presentation of variables etc.  Pay attention to
    *          the key names and ensure they are returned by the keys() method
    *          below.
    * Output : Table populated with the keys (constants) and set to default
    *          values for South African small parcel letter post rates.
    * Remarks: This is where you define, and initialise all your constants and
    *          administrator-defined variables.
    **************************************************************************/
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) VALUES ('Enable SA Domestic Parcel Post', 'MODULE_SHIPPING_SAPARCELPLUS_STATUS', 'True', 'Do you want to offer South African Domestic Parcel shipping?', '6', '0', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort order of display.', 'MODULE_SHIPPING_SAPARCELPLUS_SORT_ORDER', '0', 'Sort order of display. Lowest is displayed first.', '6', '0', now())");
     tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Tax Class', 'MODULE_SHIPPING_SAPARCELPLUS_TAX_CLASS', '0', 'Use the following tax class on the shipping fee.', '6', '0', 'tep_get_tax_class_title', 'tep_cfg_pull_down_tax_classes(', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Maximum weight allowed.', 'MODULE_SHIPPING_SAPARCELPLUS_MAX_WEIGHT', '30000', 'Maximum allowed domestic parcel weight in grams.', '6', '0', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Maximum weight for ECONOPARCEL parcels.', 'MODULE_SHIPPING_SAPARCELPLUS_ECONOPARCEL_MAX_WEIGHT', '1000', 'The weight at which EconoParcel becomes ParcelPlus.', '6', '5', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Insurance cost minimum fee.', 'MODULE_SHIPPING_SAPARCELPLUS_INSURANCE_MINIMUM', '2.00', 'The minimum allowed insurance cost.', '6', '5', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Maximum insured value.', 'MODULE_SHIPPING_SAPARCELPLUS_INSURANCE_MAXIMUM', '5000.00', 'The maximum insurable value.', '6', '5', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Insurance Rate for ECONOPARCEL parcels.', 'MODULE_SHIPPING_SAPARCELPLUS_ECONOPARCEL_INSURANCE_RATE', '2', 'The insurance cost as a percentage of the parcel value (2 = 2%). To turn off insurance, enter 0.', '6', '5', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Insurance Rate for PARCELPLUS parcels.', 'MODULE_SHIPPING_SAPARCELPLUS_PARCELPLUS_INSURANCE_RATE', '2', 'The insurance cost as a percentage of the parcel value (2 = 2%). To turn off insurance, enter 0.', '6', '5', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('EconoParcel base shipping rate', 'MODULE_SHIPPING_SAPARCELPLUS_ECONOPARCEL_BASE_RATE', '12.60', 'EconoParcel base rate. Example: 12.60', '6', '5', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('EconoParcel unit shipping rate per kilo after the first (set to 0)', 'MODULE_SHIPPING_SAPARCELPLUS_ECONOPARCEL_UNIT_RATE', '0', 'EconoParcel unit rate (should stay 0). Example: 0', '6', '5', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('ParcelPlus base shipping rate', 'MODULE_SHIPPING_SAPARCELPLUS_PARCELPLUS_BASE_RATE', '23.65', 'ParcelPlus base rate (fee for first kilo). Example: 23.65', '6', '5', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('ParcelPlus unit shipping rate per kilo after the first', 'MODULE_SHIPPING_SAPARCELPLUS_PARCELPLUS_UNIT_RATE', '3.05', 'ParcelPlus unit rate (for each kilo after the first one). Example: 3.05', '6', '5', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Handling Fee', 'MODULE_SHIPPING_SAPARCELPLUS_HANDLING_FEE', '0', 'Handling Fee for SA domestic parcels', '6', '1', now())");
    }

    function remove() { 
    /**************************************************************************
    *                                Remove                                   *
    * Purpose: Deletes all the configuration keys
    **************************************************************************/
      tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }

    function keys() {
    /**************************************************************************
    *                                Remove                                   *
    * Purpose: Returns all the keys used by this module.
    **************************************************************************/
      $keys = array('MODULE_SHIPPING_SAPARCELPLUS_STATUS', 
                    'MODULE_SHIPPING_SAPARCELPLUS_SORT_ORDER', 
                    'MODULE_SHIPPING_SAPARCELPLUS_TAX_CLASS', 
                    'MODULE_SHIPPING_SAPARCELPLUS_MAX_WEIGHT',
                    'MODULE_SHIPPING_SAPARCELPLUS_HANDLING_FEE',
                    'MODULE_SHIPPING_SAPARCELPLUS_INSURANCE_MAXIMUM',
                    'MODULE_SHIPPING_SAPARCELPLUS_INSURANCE_MINIMUM',
                    'MODULE_SHIPPING_SAPARCELPLUS_ECONOPARCEL_MAX_WEIGHT',
                    'MODULE_SHIPPING_SAPARCELPLUS_ECONOPARCEL_INSURANCE_RATE',
                    'MODULE_SHIPPING_SAPARCELPLUS_PARCELPLUS_INSURANCE_RATE',
                    'MODULE_SHIPPING_SAPARCELPLUS_ECONOPARCEL_BASE_RATE',
                    'MODULE_SHIPPING_SAPARCELPLUS_ECONOPARCEL_UNIT_RATE',
                    'MODULE_SHIPPING_SAPARCELPLUS_PARCELPLUS_BASE_RATE',
                    'MODULE_SHIPPING_SAPARCELPLUS_PARCELPLUS_UNIT_RATE'
                    );

      return $keys;
    }
  }
?>
